package com.white.androidstudy.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by ytf on 2017/12/3 003.
 * Description:
 */

public class WatchPannel extends AbsView
{

    private Paint mPaint;
    private Paint mTextPaint;
    private Paint mTimePointerPaint;

    public WatchPannel(Context context)
    {
        super(context);
        init(context);
    }

    private void init(Context context)
    {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);

        mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setTextAlign(Paint.Align.CENTER);

        mTimePointerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mTimePointerPaint.setStyle(Paint.Style.STROKE);
    }

    public WatchPannel(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init(context);
    }

    @Override
    protected boolean needWidthHeightEquals()
    {
        return true;
    }

    @Override
    public int getDefaultWrapWidth()
    {
        return 300;
    }

    @Override
    public int getDefaultWrapHeight()
    {
        return 300;
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        drawScale(canvas);
        drawPointer(canvas);
        postInvalidateDelayed(1000);
    }



    private void drawPointer(Canvas canvas)
    {
        //获取系统当前时间
        SimpleDateFormat format = new SimpleDateFormat("HH-mm-ss");
        String time = format.format(new Date(System.currentTimeMillis()));
        String[] split = time.split("-");
        int hour = Integer.parseInt(split[0]);
        int minute = Integer.parseInt(split[1]);
        int second = Integer.parseInt(split[2]);
        //时针走过的角度
        int hourAngle = hour * 30 + minute / 2;
        //分针走过的角度
        int minuteAngle = minute * 6 + second / 30;
        //秒针走过的角度
        int secondAngle = second * 6;
        Log.e("s", "second = " + second + "  angle = " + secondAngle);
        //绘制时钟,以12整点为0°参照点
        mTimePointerPaint.setStrokeWidth(10);
        mTimePointerPaint.setColor(Color.BLACK);
        canvas.rotate(hourAngle, centerX, centerY);
        canvas.drawLine(centerX, centerY, centerX, centerY -150, mTimePointerPaint);
        canvas.save();
        canvas.restore();
        //这里画好了时钟，我们需要再将画布转回来,继续以12整点为0°参照点
        canvas.rotate(-hourAngle, centerX, centerY);

        //绘制分钟
        mTimePointerPaint.setStrokeWidth(5);
        mTimePointerPaint.setColor(Color.BLACK);
        canvas.rotate(minuteAngle, centerX, centerY);
        canvas.drawLine(centerX, centerY, centerX, centerY - 200, mTimePointerPaint);
        canvas.save();
        canvas.restore();
        //这里同上
        canvas.rotate(-minuteAngle, centerX, centerY);

        //绘制秒钟
        mTimePointerPaint.setStrokeWidth(2);
        mTimePointerPaint.setColor(Color.RED);
        canvas.rotate(secondAngle, centerX, centerY);
        canvas.drawLine(centerX, centerY, centerX, centerY - 300, mTimePointerPaint);
        canvas.rotate(-secondAngle, centerX, centerY);
        canvas.save();
        canvas.restore();
    }

    private void drawScale(Canvas canvas)
    {
        mPaint.setColor(Color.BLACK);
        mPaint.setStrokeWidth(scaleStrokeWidth);
        canvas.drawCircle(centerX, centerY, centerX - halfScaleStrokeWidth, mPaint);
        canvas.drawCircle(centerX, centerY, halfScaleStrokeWidth, mPaint);

        for(Line l :lineList)
        {
            l.draw(canvas, mPaint);
        }
    }

    private void drawOutline(Canvas canvas)
    {
        mPaint.setStrokeWidth(3);
        for (int i = 0; i < 24; i++)
        {
            String drawNumber;
            if (i % 2 == 0)
            {
                if (i == 0)
                    drawNumber = "12";
                else
                    drawNumber = String.valueOf(i / 2);
                mPaint.setStrokeWidth(5);
                mPaint.setTextSize(20);
                canvas.drawLine(centerX, 0, centerX, 50, mPaint);
                mPaint.setStrokeWidth(1);
                canvas.drawText(drawNumber, centerX - mPaint.measureText(drawNumber) / 2, 75, mPaint);
            } else
            {
                mPaint.setStrokeWidth(3);
                canvas.drawLine(centerX, 0, centerX, 30, mPaint);
            }
            canvas.rotate(15, centerX, centerY);
        }
    }

    int width, height;
    int centerX, centerY;
    int shortScale, longScale;
    int halfScaleStrokeWidth, scaleStrokeWidth;

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh)
    {
        super.onSizeChanged(w, h, oldw, oldh);
        width = getMeasuredWidth();
        height = getMeasuredHeight();
        centerX = width / 2;
        centerY = height / 2;
        shortScale = width / 30;
        longScale = (int) (shortScale * 1.3f);
        halfScaleStrokeWidth = width / 250;
        scaleStrokeWidth = halfScaleStrokeWidth * 2;

        mTextPaint.setTextSize(width / 20);
        mTextPaint.setColor(Color.RED);

        generatePoint();
    }

    private void initTime()
    {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        String time = sdf.format(new Date(System.currentTimeMillis()));
        String times[] = time.split(":", -1);
    }

    @Override
    public void onGlobalLayout()
    {

    }

    private List<Line> lineList = new ArrayList<>();

    //角度从0度顺时针加
    private void generatePoint()
    {
        float RADIAN = (float) (Math.PI / 180);
        float unitRadian = RADIAN * 15;

        lineList.clear();
        int radiusOut = centerX - halfScaleStrokeWidth;
        int radiusInLong = centerX - longScale;
        int radiusInShort = centerX - shortScale;
        int radiusInTxt = radiusInLong - 30;
        int j = 0;
        for (int i = 0; i < 24; i++)
        {
            float ox = (float) (centerX + radiusOut * Math.cos(i * unitRadian));
            float oy = (float) (centerY + radiusOut * Math.sin(i * unitRadian));
            Line l;
            float ix, iy;
            float txtX, txtY;
            if (i == 0)
            {
                ix = (float) (centerX + radiusInLong * Math.cos(i * unitRadian));
                iy = (float) (centerY + radiusInLong * Math.sin(i * unitRadian));

                txtX = (float) (centerX + radiusInTxt * Math.cos(i * unitRadian));
                txtY = (float) (centerY + radiusInTxt * Math.sin(i * unitRadian));
                l = new Line(ix, iy, ox, oy, scaleStrokeWidth);
                j = 3;
                l.setNumberPosi(txtX, txtY);
                l.setNum(j);
            }else
            {
                if(i % 2 == 0)
                {
                    ix = (float) (centerX + radiusInLong * Math.cos(i * unitRadian));
                    iy = (float) (centerY + radiusInLong * Math.sin(i * unitRadian));
                    l = new Line(ix, iy, ox, oy, scaleStrokeWidth);

                    txtX = (float) (centerX + radiusInTxt * Math.cos(i * unitRadian));
                    txtY = (float) (centerY + radiusInTxt * Math.sin(i * unitRadian));
                    l.setNumberPosi(txtX, txtY);
                    if(i <= 18)
                    {
                        j++;
                        l.setNum(j);
                    }else
                    {
                        if(i == 20) j = 0;
                        j++;
                        l.setNum(j);
                    }
                }else
                {
                    ix = (float) (centerX + radiusInShort * Math.cos(i * unitRadian));
                    iy = (float) (centerY + radiusInShort * Math.sin(i * unitRadian));
                    l = new Line(ix, iy, ox, oy, halfScaleStrokeWidth);
                }
            }

            lineList.add(l);
        }
    }

    class Line
    {
        float startX, startY, endX, endY;
        int strokeWidth;

        float txtX, txtY;
        int num;

        public Line(float startX, float startY, float endX, float endY, int strokeWidth)
        {
            this.startX = startX;
            this.startY = startY;
            this.endX = endX;
            this.endY = endY;
            this.strokeWidth = strokeWidth;
        }

        public void setNum(int num)
        {
            this.num = num;
        }

        public void draw(Canvas canvas, Paint pt)
        {
            pt.setStrokeWidth(strokeWidth);
            canvas.drawLine(startX, startY, endX, endY, pt);

            if(num != 0)
            {
                Paint.FontMetrics fm = mTextPaint.getFontMetrics();
                float baseline = txtY - fm.ascent - (fm.descent - fm.ascent) / 2;
                canvas.drawText(String.valueOf(num), txtX, baseline, mTextPaint);
            }
        }

        public void setNumberPosi(float x, float y)
        {
            this.txtX = x;
            this.txtY = y;
        }
    }
}
